package com.wms.service.impl;

import com.wms.entity.OrderPurchase;
import com.wms.entity.OrderPurchaseDTO;
import com.wms.mapper.OrderPurchaseMapper;
import com.wms.service.OrderPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * ---Do First---
 *
 * @author:wellseasun
 * @date:2025/4/27 下午 8:30
 * @desc:
 */
@Service
public class OrderPurchaseServiceImpl implements OrderPurchaseService {
    @Autowired
    private OrderPurchaseMapper orderPurchaseMapper;
    @Override
    public List<OrderPurchase> findAll() {
        return orderPurchaseMapper.findAll();
    }

    @Override
    public List<OrderPurchase> findAllByPage(OrderPurchaseDTO orderPurchaseDTO) {
        return orderPurchaseMapper.findAllByPage(orderPurchaseDTO);
    }

    @Override
    public Boolean add(OrderPurchase orderPurchase) {
        // // 方式1
        // String filePath = orderPurchase.getGoodImageUrl();
        // // 分割路径并获取最后两位
        // List<String> pathSegments = Arrays.asList(filePath.split("/"));
        // int size = pathSegments.size();
        // // 安全获取最后两个元素（当元素不足两个时取全部）
        // List<String> lastTwoSegments = pathSegments.subList(
        //         Math.max(size - 2, 0),  // 起始索引不小于0
        //         size
        // );
        // // 合并为字符串（可根据需要修改连接符）
        // String result = String.join("/", lastTwoSegments);
        //方式2
        String result = convertImageUrl(orderPurchase.getGoodImageUrl());
        orderPurchase.setGoodImageUrl(result);
        return orderPurchaseMapper.add(orderPurchase);
    }

    @Override
    public Boolean deleteOrderPurchase(List<Integer> ids) {
        return orderPurchaseMapper.deleteOrderPurchase(ids);
    }

    @Override
    public Boolean updateOrderPurchase(OrderPurchase orderPurchase) {
        String result = convertImageUrl(orderPurchase.getGoodImageUrl());
        orderPurchase.setGoodImageUrl(result);
        return orderPurchaseMapper.updateOrderPurchase(orderPurchase);
    }

    @Override
    public void insertBatch(List<OrderPurchase> cachedDataList) {
        orderPurchaseMapper.insertBatch(cachedDataList);
    }

    // 转换图片地址
    private String convertImageUrl(String filePath) {
        // 分割路径并获取最后两位
        List<String> pathSegments = Arrays.asList(filePath.split("/"));
        int size = pathSegments.size();
        // 安全获取最后两个元素（当元素不足两个时取全部）
        List<String> lastTwoSegments = pathSegments.subList(
                Math.max(size - 2, 0),  // 起始索引不小于0
                size
        );
        // 合并为字符串（可根据需要修改连接符）
        String result = String.join("/", lastTwoSegments);
        return result;
    }
}
